package slap.exception;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.concurrent.atomic.AtomicLong;


import jodd.format.Printf;
import jodd.util.StringUtil;

public class SlapException extends RuntimeException {

    static AtomicLong atomicLong = new AtomicLong(System.currentTimeMillis());
    String id;

    public SlapException() {
        super();
        setId();
    }

    public SlapException(String message) {
        super(message);
        setId();
    }

    public SlapException(String message, Throwable cause) {
        super(message, cause);
        setId();
    }

    public SlapException(Throwable cause) {
    	super(cause);
        setId();
	}

	void setId() {
        long nid = atomicLong.incrementAndGet();
        id = Long.toString(nid, 26);
    }

    public String getErrorTitle(){
    	return  this.getCause().getClass().getName();
    }

    public String getErrorDescription(){
    	return Printf.str("<strong>%s</strong>", this.getMessage().substring(getMessage().indexOf(':')+1));
    }

    public Integer getLineNumber() {
        return -1;
    }

    public String getSourceFile() {
        return "";
    }

    public String getId() {
        return id;
    }

    public String getMoreHTML() {
    	StringWriter sw = new StringWriter();
    	this.printStackTrace(new PrintWriter(sw));
        return sw.toString().replaceAll(" ", "&nbsp;")
					        .replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
					        .replaceAll("\n", "<br/>");
        					
    }
}